package j.framework.core.entity;

import j.util.New;
import j.util.StringUtils;
import j.util.TwoTuple;

import java.util.List;
import java.util.Set;

/**
 * 实体键
 * Created by junhai on 2015/2/25.
 */
public class EntityKey implements java.io.Serializable {
    /**
     * 主键标记
     */
    public static final String ENTITY_KEY_PRIMARY = "P";
    /**
     * 唯一键标记
     */
    public static final String ENTITY_KEY_UNIQUE = "U";
    private String keyName;
    private String keyType;

    //检测是否重复的增加语句及参数顺序
    private String checkDoubleForInsertedSql;
    private List<String> checkDoubleForInsertedArgsIndex;

    //检测是否重复的修改语句及参数顺序
    private String checkDoubleForUpdatedSql;
    private List<String> checkDoubleForUpdatedArgsIndex;

    /**
     * 获取此唯一键的测重sql语句及参数顺序
     * @param entity 实体
     * @return
     */
    public TwoTuple<String,List<String>> getCheckDoubleForInsertedSql(Entity entity){
        if(StringUtils.isNullOrEmpty(checkDoubleForInsertedSql)){
            synchronized (EntityKey.class){
                if(StringUtils.isNullOrEmpty(checkDoubleForInsertedSql)){
                    checkDoubleForInsertedArgsIndex = New.list();
                    checkDoubleForInsertedSql = "SELECT COUNT(*) FROM "+entity.getTableName()+" WHERE ";
                    int i=0;
                    for(String key : tableFieldNames){
                        if(i++>0){
                            checkDoubleForInsertedSql += " AND ";
                        }
                        checkDoubleForInsertedSql += key + " =? ";
                        checkDoubleForInsertedArgsIndex.add(key);
                    }
                }
            }
        }
        return New.tuple(checkDoubleForInsertedSql, checkDoubleForInsertedArgsIndex);
    }

    /**
     * 获取此唯一键的测重sql语句及参数顺序
     * @param entity 实体
     * @return
     */
    public TwoTuple<String,List<String>> getCheckDoubleForUpdatedSql(Entity entity){
        if(StringUtils.isNullOrEmpty(checkDoubleForUpdatedSql)){
            synchronized (EntityKey.class){
                if(StringUtils.isNullOrEmpty(checkDoubleForUpdatedSql)){
                    checkDoubleForUpdatedArgsIndex = New.list();
                    checkDoubleForUpdatedSql = "SELECT COUNT(*) FROM "+entity.getTableName()+" WHERE ";
                    int i=0;
                    for(String key : entity.primaryKeys){
                        if(i++>0){
                            checkDoubleForUpdatedSql += " AND ";
                        }
                        checkDoubleForUpdatedSql += key + " !=? ";
                        checkDoubleForUpdatedArgsIndex.add(key);
                    }
                    for(String key : tableFieldNames){
                        checkDoubleForUpdatedSql += " AND ";
                        checkDoubleForUpdatedSql += key + " =? ";
                        checkDoubleForUpdatedArgsIndex.add(key);
                    }
                }
            }
        }
        return New.tuple(checkDoubleForUpdatedSql, checkDoubleForUpdatedArgsIndex);
    }

    //对应的是数据表中的字段名称
    public final Set<String> tableFieldNames = New.set();

    public String getKeyName() {
        return keyName;
    }
    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }

    public String getKeyType() {
        return keyType;
    }
    public void setKeyType(String keyType) {
        this.keyType = keyType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EntityKey entityKey = (EntityKey) o;

        if (keyName != null ? !keyName.equals(entityKey.keyName) : entityKey.keyName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return keyName != null ? keyName.hashCode() : 0;
    }
}
